<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\StaffJurisdictionModel;
use app\model\LeftMenuModel;
use app\model\StaffRoleModel;
use app\model\StaffModel;
use think\facade\Request;
use think\facade\Validate;

class StaffJurisdiction
{
    //权限列表获取
    public function list()
    {
        $staffJurisdictionList =StaffJurisdictionModel::select();
        foreach ($staffJurisdictionList as &$value){
            //菜单名称
            $leftMenuOne = LeftMenuModel::find($value['left_menu_id']);
            $value['left_menu_name'] = $leftMenuOne['left_menu_name'];
            
            //角色名称
            $staffRoleOne = StaffRoleModel::find($value['role_id']);
            $value['role_name'] = $staffRoleOne['role_name'];
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'staffJurisdictionList' => $staffJurisdictionList,
            ],
        ];
        return json($data);
    }
    public function add()
    {

        //order增加权限
        $params = Request::param();
        $validate = Validate::rule([
            'left_menu_id|菜单id' => 'require',
//            'password|密码' => 'require',
            'role_id|角色id' => 'require',
//            'staff_id|员工id' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
        $staffJurisdictionOne = StaffJurisdictionModel::where('left_menu_id','=',$params['left_menu_id'])->where('role_id','=',$params['role_id'])->find();
        if($staffJurisdictionOne){
            $data = [
            'status' => 10001,
            'message' => '该权限已存在',
            'data' => []
        ];
        return json($data);
        }
        $params['add_time']=$params['update_time']=time();
        $result=StaffJurisdictionModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    // 角色删除
    public function delete()
    {
        $jurisdictionId = Request::param("jurisdiction_id");
        $validate = Validate::rule([
            'jurisdiction_id|权限id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['jurisdiction_id' => $jurisdictionId])) {
            echo $validate->getError();
            exit();
        }
//        $role=StaffJurisdictionModel::find($jurisdictionId);
        $result = StaffJurisdictionModel::destroy($jurisdictionId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //角色修改
    public function update()
    {
        $params = Request::param();
        $validate = Validate::rule([
            'jurisdiction_id|权限id'=>'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['jurisdiction_id'=>$params['jurisdiction_id']])) {
            echo $validate->getError();
            exit();
        }

        $jurisdiction = StaffJurisdictionModel::find($params['jurisdiction_id']);
//
        if ($params['left_menu_id']){
            $jurisdiction['left_menu_id']=$params['left_menu_id'];
        }

        if ($params['role_id']){
            $jurisdiction['role_id']=$params['role_id'];
        }

//        if ($params['password']){
//            $staff['password']=$params['password'];
//        }
//        if ($params['role_id']){
//            $staff['role_id']=$params['role_id'];
//        }
        $jurisdiction['update_time'] = time();
        $result = $jurisdiction->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
    
    //获取权限号码
    public function getJurCode()
    {
        
        $staffId = Request::param("staff_id");
        $validate = Validate::rule([
            'staff_id|员工id' => 'require'
        ]);
        if (!$validate->check(['staff_id' => $staffId])) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }
        
        $staffOne = StaffModel::where('staff_id','=',$staffId)->find();
        $StaffJurisdictionList = StaffJurisdictionModel::where('role_id','=',$staffOne['role_id'])->select();
        $jurisdictionCode = [];
        foreach ($StaffJurisdictionList as &$value){
            $leftMenuOne = LeftMenuModel::where('left_menu_id','=',$value['left_menu_id'])->find();
            $jurisdictionCode[] = $leftMenuOne['left_menu_code'];
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'jurisdictionCode' => $jurisdictionCode,
                'staff_id' => $staffId,
                'role_id' => $staffOne['role_id']
            ],
        ];
        return json($data);
    }
}
